package com.zjc.netty.client;

import com.zjc.netty.client.handler.ClientHandler;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.timeout.IdleStateHandler;

public class CustomChannelInitializer extends ChannelInitializer {

    private CustomChannelPool customChannelPool;

    public CustomChannelInitializer(CustomChannelPool customChannelPool) {
        this.customChannelPool = customChannelPool;
    }

    @Override
    protected void initChannel(Channel channel) {
        channel.pipeline().addLast(new LoggingHandler(LogLevel.INFO));
        channel.pipeline().addLast(new IdleStateHandler(60, 10, 60));
        channel.pipeline().addLast(new LengthFieldBasedFrameDecoder(65533, 0, 2, 0, 2));
        channel.pipeline().addLast(new StringDecoder());
        channel.pipeline().addLast(new LengthFieldPrepender(2));
        channel.pipeline().addLast(new StringEncoder());
        channel.pipeline().addLast(new ClientHandler(customChannelPool));
    }
}
